import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { WorkPlan, Material, WorkOrderData, Stencil, Fixture, SOPApproval } from '../types'

export const useDataStore = defineStore('data', () => {
  const workPlans = ref<WorkPlan[]>([])
  const materials = ref<Material[]>([])
  const workOrderData = ref<WorkOrderData[]>([])
  const stencils = ref<Stencil[]>([])
  const fixtures = ref<Fixture[]>([])
  const sopApprovals = ref<SOPApproval[]>([])

  // 初始化示例数据
  function initSampleData() {
    workPlans.value = [
      {
        id: '1',
        model: 'Model-A',
        workOrder: 'WO-2024-001',
        quantity: 100,
        date: '2024-11-15',
        notes: '紧急订单',
        status: 'pending'
      },
      {
        id: '2',
        model: 'Model-B',
        workOrder: 'WO-2024-002',
        quantity: 200,
        date: '2024-11-20',
        notes: '常规订单',
        status: 'in-progress'
      }
    ]

    materials.value = [
      {
        id: '1',
        serial: 1,
        model: 'Model-A',
        name: 'PCB主板',
        quantity: 100,
        location: '仓库A-1',
        notes: '已检验'
      },
      {
        id: '2',
        serial: 2,
        model: 'Model-B',
        name: '电阻包',
        quantity: 500,
        location: '仓库B-2',
        notes: '待检验'
      }
    ]

    workOrderData.value = [
      {
        id: '1',
        workOrder: 'WO-2024-001',
        workOrderType: '生产工单',
        partNumber: 'PART-001',
        partName: '主控板',
        specification: 'V1.0',
        category: '电子类',
        workOrderQuantity: 100,
        version: '1.0',
        productionDate: '2024-11-15',
        process: 'SMT贴片',
        stencilTop: true,
        stencilBot: false,
        reflowFixture: true,
        waveFixture: false,
        pressFixture: true,
        bom: true,
        sop: true,
        productionMaterials: true,
        type: '标准'
      }
    ]

    stencils.value = [
      {
        id: '1',
        serial: 1,
        model: 'Model-A',
        quantity: 1,
        openTime: '2024-11-10',
        acceptanceTime: '2024-11-12',
        status: 'completed',
        notes: '已验收通过'
      }
    ]

    fixtures.value = [
      {
        id: '1',
        serial: 1,
        model: 'Model-A',
        quantity: 2,
        openTime: '2024-11-08',
        acceptanceTime: '2024-11-11',
        status: 'completed',
        notes: '回流焊治具'
      }
    ]

    sopApprovals.value = [
      {
        id: '1',
        serial: 1,
        model: 'Model-A',
        status: 'approved',
        submitTime: '2024-11-05',
        approvalTime: '2024-11-10',
        notes: 'SOP已签核'
      }
    ]
  }

  // 检查延期工单
  function checkDelayedOrders() {
    const today = new Date()
    return workPlans.value.filter(plan => {
      const planDate = new Date(plan.date)
      return planDate < today && plan.status !== 'completed'
    })
  }

  return {
    workPlans,
    materials,
    workOrderData,
    stencils,
    fixtures,
    sopApprovals,
    initSampleData,
    checkDelayedOrders
  }
})
